var pageii = null;
var grid = null;
$(function() {

    grid = lyGrid({
        pagId : 'paging',
        l_column : [  {
            colkey : "deviceMaintainId",
            name : "id",
            hide : true
        },{
            colkey : "devicemaintaincode",
            name : "设备维修编号",
        }, {
            colkey : "deviceCode",
            name : "设备编码",
        },  {
            colkey : "deviceName",
            name : "设备名称",
        }, {
            colkey : "empName",
            name : "报修人",
        }, {
            colkey : "deviceMaintainDate",
            name : "报修时间",
            renderData : function(rowindex,data, rowdata, column) {
                return new Date(data).format("yyyy-MM-dd");
            },
        }, {
            colkey : "deviceMaintainstate",
            name : "维修状态",
            renderData : function(rowindex,data, rowdata, column) {
                if(data == 1){
                    return "修理完成";
                }else if(data == 2){
                    return "不可维修";
                }else if(data == 3){
                    return "维修失败";
                }else {
                    return "未维修";
                }
            }
        }, {
            colkey : "deviceMaintainCause",
            name : "故障原因"
        }, {
            colkey : "deviceMaintainResult",
            name : "故障处理"
        }, {
            colkey : "note",
            name : "备注"
        }],
        jsonUrl : rootPath + '/devicemaintain/maintain_findByPage.shtml',
        dymCol:true,
        checkbox : true,
        records: "list",
        checkValue: "deviceMaintainId",
        serNumber : false,
        totalPages: "pages",
        totalRecords: "total",
        pagecode: '10',
        pageNow : 'pageNum'
    });

    $("#addAccount").click("click", function() {
        addAccount();
    });
    $("#editAccount").click("click", function() {
        editAccount();
    });
    $("#delAccount").click("click", function() {
        delAccount();
    });
    $("#verify").click("click", function() {
        verify();
    });
    $("#search").click("click", function() {
        var searchParams = $("#deviceMaintainEmp").serializeJson();
        grid.setOptions({
            data : searchParams
        });
    });
    $("#replace").click("click", function() {// 绑定重置按扭
        $("#deviceMaintainEmp").val("");
    });
});

function verify() {
    var cbox = grid.getSelectedCheckbox();
    if (cbox.length > 1 || cbox == "") {
        layer.msg("只能选中一个");
        return;
    }
    pageii = layer.open({
        title : "编辑设备维修",
        type : 2,
        area : [ "800px", "100%" ],
        content : rootPath + '/devicemaintain/maintain_verifyUI.shtml?id=' + cbox
    });
}

function editAccount() {
    var cbox = grid.getSelectedCheckbox();
    if (cbox.length > 1 || cbox == "") {
        layer.msg("只能选中一个");
        return;
    }
    pageii = layer.open({
        title : "编辑设备维修",
        type : 2,
        area : [ "800px", "100%" ],
        content : rootPath + '/devicemaintain/maintain_editUI.shtml?id=' + cbox
    });
}
function addAccount() {
    pageii = layer.open({
        title : "新增设备维修",
        type : 2,
        area : [ "800px", "100%" ],
        content : rootPath + '/devicemaintain/maintain_addUI.shtml'
    });
}
function delAccount() {
    var cbox = grid.getSelectedCheckbox();
    console.log("cbox:"+cbox)
    if (cbox == "") {
        layer.msg("请选择删除项！！");
        return;
    }
    layer.confirm('是否删除？', function(index) {
        var url = rootPath + '/devicemaintain/maintain_deleteEntity.shtml';
        var s = CommnUtil.ajax(url, {
            ids : cbox.join(",")
        }, "json");
        if (s == "success") {
            layer.msg('删除成功');
            grid.loadData();
        } else {
            layer.msg('删除失败');
        }
    });
}

function exportExcel(excelName){
    // var mytable = "";//需要导出的数据
    var fileName = excelName+".xlsx"

    var cbox = grid.getSelectedCheckbox();
    if (cbox == "") {//没选则导出所有数据
        var mytable = document.getElementById("mytable");//获取table
        var sheet = XLSX.utils.table_to_sheet(mytable);
    } else {//第二种：导出指定的行

        //获取表头标题，就是表中第一行，序号不存在
        var valueTd= document.getElementById ("mytable").rows[0];

        //复制节点
        var clone_valueTd = valueTd.cloneNode(true);
        // debugger;

        //根据选中行，动态拼接table
        // var tableData = "<table><tr>" + valueTd.innerHTML + "</tr>";
        var table = document.createElement('table');
        table.id = "table1";
        table.appendChild(clone_valueTd);//使用原有节点会对原有dom造成影响

        //获取所有表中所有checkbox
        var inputs = document.getElementById("mytable").getElementsByTagName("input");
        console.log("inputs.length:"+inputs.length);
        for(var i=0;i<inputs.length;i++) {//获取选中行
            if (inputs[i].type == "checkbox") {
                if (inputs[i].checked) {
                    var checkedRow = inputs[i];
                    var tr = checkedRow.parentNode.parentNode;
                    var clone_tr = tr.cloneNode(true);//复制
                    console.log(tr.cells[0].innerText);
                    if(tr.cells[0].innerText != ""){//序号不为空则拼接，为空说明是表头标题
                        // tableData += "<tr>" + tr.innerHTML + "</tr>";
                        table.appendChild(clone_tr)
                    }
                    // console.log("为空是表头，不拼接")
                    // console.log("tr...:"+tr);
                }
            }
        }

        // tableData += "</table>";
        // mytable = document.createElement(tableData);
        var sheet = XLSX.utils.table_to_sheet(table);
    }

    openDownloadDialog(sheet2blob(sheet), fileName);
}